package org.hxzon.workeasy.service.impl;

import java.util.Collection;
import java.util.List;

import org.hxzon.project.AbstractModelService;
import org.hxzon.util.MD5Util;
import org.hxzon.util.StringUtil;
import org.hxzon.util.cache.FifoCache;
import org.hxzon.workeasy.model.EasyUzer;
import org.hxzon.workeasy.service.EasyUserService;
import org.springframework.stereotype.Service;

@Service
public class EasyUserServiceImpl extends AbstractModelService<EasyUzer> implements EasyUserService {
    private FifoCache<String, EasyUzer> userCache = new FifoCache<String, EasyUzer>(100);

    public void save(EasyUzer user) {
        super.save(user);
        userCache.add(user.getUsername(), user);
    }

    public void saveAll(Collection<EasyUzer> users) {
        for (EasyUzer user : users) {
            save(user);
        }
    }

    public void delete(EasyUzer user) {
        super.delete(user);
        userCache.remove(user.getUsername());
    }

    public EasyUzer findById(String id) {
        EasyUzer result = super.findById(id);
        if (result != null) {
            userCache.add(result.getUsername(), result);
        }
        return result;
    }

    public EasyUzer findByUsername(String username) {
        if (username == null || username.isEmpty()) {
            return null;
        }
        if (EasyUzer.Username_Guess.equals(username)) {
            return null;//return null not return new Uzer()
        }
        EasyUzer result = userCache.get(username);
        if (result != null) {
            return result;
        }
        username = StringUtil.escapeSql(username);
        List<EasyUzer> list = dao.findList(EasyUzer.class, " where p.username='" + username + "'", -1, -1);
        if (list.isEmpty()) {
            return null;
        }
        if (list.size() > 1) {
            logger.error("user[{}] not unique", username);
        }
        result = list.get(0);
        userCache.add(result.getUsername(), result);
        return result;
    }

    public EasyUzer findByUsernameAndPassword(String username, String password) {
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            return null;
        }
        if (password.length() < 32) {
            password = MD5Util.toMD5(username + password);
        }
        EasyUzer result = findByUsername(username);
        if (result != null) {
            if (password.equals(result.getPassword())) {
                return result;
            }
        }
        return null;
    }

    public boolean isAdmin(String username) {
        EasyUzer user = findByUsername(username);
        return user != null && user.getAdminGrade() == 0;
    }

    public boolean isAdmins(String username) {
        EasyUzer user = findByUsername(username);
        return user != null && user.getAdminGrade() <= 1;
    }

    public List<EasyUzer> findByDepartment(int department) {
        return dao.findList(EasyUzer.class, " where p.department=" + department + " ");
    }

    public boolean existUsername(String username) {
        return (findByUsername(username) != null);
    }

}
